//
//  ParamModel.m
//  国付宝网络请求参数示例
//
//  Created by winson cheung on 15/12/9.
//  Copyright © 2015年 winson cheung. All rights reserved.
//

#define NSStringValue(dictionary, key) [NSString stringWithFormat:@"%@", dictionary[key]]

#import "ParamModel.h"
#import "YSMD5.h"

@implementation ParamModel

/*
 * 使用方法:
 *  1. 先创建signValue加密字符串
 *  ex: NSDictionary *dic  = ["version":"...", ...];
 *      NSString *signVaule = [ParamModel signValueWithDictionary:dic];
 *
 *  2. 再创建参数字典用createParamMoel
 *  ex: NSMutableDictionary *param = [ParamModel createParamMoelWithVersion:version
 *                                                        charset:charset
 *                                                        language:language
 *                                                        signType:signType
 *                                                        tranCode:tranCode
 *                                                        merchantID:merchantID
 *                                                        merOrderNum:merOrderNum
 *                                                        tranAmt:tranAmt
 *                                                        feeAmt:feeAmt
 *                                                        currencyType:currencyType
 *                                                        frontMerUrl:frontMerUrl
 *                                                        backgroundMerUrl:backgroundMerUrl
 *                                                        tranDateTime:tranDateTime
 *                                                        virCardNoIn:virCardNoIn
 *                                                        tranIP:tranIP
 *                                                        isRepeatSubmit:isRepeatSubmit
 *                                                        goodsName:goodsName
 *                                                        goodsDetail:goodsDetail
 *                                                        buyerName:buyerName
 *                                                        buyerContact:buyerContact
 *                                                        merRemark:merRemark
 *                                                        mobileSighValue:mobileSighValue
 *                                                        signValue:signVaule];
 *
 *
 */


// 创建参数字典
+ (id)createParamMoelWithVersion:(NSString *)version
                         charset:(NSString *)charset
                        language:(NSString *)language
                        signType:(NSString *)signType
                        tranCode:(NSString *)tranCode
                      merchantID:(NSString *)merchantID
                     merOrderNum:(NSString *)merOrderNum
                         tranAmt:(NSString *)tranAmt
                          feeAmt:(NSString *)feeAmt
                    currencyType:(NSString *)currencyType
                     frontMerUrl:(NSString *)frontMerUrl
                backgroundMerUrl:(NSString *)backgroundMerUrl
                    tranDateTime:(NSString *)tranDateTime
                     virCardNoIn:(NSString *)virCardNoIn
                          tranIP:(NSString *)tranIP
                  isRepeatSubmit:(NSString *)isRepeatSubmit
                       goodsName:(NSString *)goodsName
                     goodsDetail:(NSString *)goodsDetail
                       buyerName:(NSString *)buyerName
                    buyerContact:(NSString *)buyerContact
                       merRemark:(NSString *)merRemark
                 mobileSighValue:(NSString *)mobileSighValue
                       signValue:(NSString *)signValue
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:version forKey:@"version"];
    // charset可传nil 默认为1
    [param setValue:charset forKey:@"charset"];
    [param setValue:language forKey:@"language"];
    // signType可传nil 默认为1
    [param setValue:signType forKey:@"signType"];
    [param setValue:tranCode forKey:@"tranCode"];
    [param setValue:merchantID forKey:@"merchantID"];
    [param setValue:merOrderNum forKey:@"merOrderNum"];
    [param setValue:tranAmt forKey:@"tranAmt"];
    [param setValue:feeAmt forKey:@"feeAmt"];
    [param setValue:currencyType forKey:@"currencyType"];
    // 接口文档上说传nil
    [param setValue:frontMerUrl forKey:@"frontMerUrl"];
    [param setValue:backgroundMerUrl forKey:@"backgroundMerUrl"];
    [param setValue:tranDateTime forKey:@"tranDateTime"];
    [param setValue:virCardNoIn forKey:@"virCardNoIn"];
    [param setValue:tranIP forKey:@"tranIP"];
    // isRepeatSubmit可传nil 默认为1
    [param setValue:isRepeatSubmit forKey:@"isRepeatSubmit"];
    // goodsName可传nil
    [param setValue:goodsName forKey:@"goodsName"];
    // goodsDetail可传nil
    [param setValue:goodsDetail forKey:@"goodsDetail"];
    [param setValue:buyerName forKey:@"buyerName"];
    [param setValue:buyerContact forKey:@"buyerContact"];
    // merRemark可传nil
    [param setValue:merRemark forKey:@"merRemark"];
    [param setValue:mobileSighValue forKey:@"mobileSighValue"];
    
    // signValue可传nil, 待signValue创建好后, 再add到param中
    [param setValue:signValue forKey:@"signValue"];
    return param;
    
}

// 创建signValue md5 加密字符串
+ (NSString *)signValueWithDictionary:(NSDictionary *)dic {
    
    NSString *signVaule = [NSString stringWithFormat:@"version=[%@]tranCode=[%@]merchantID=[%@]merOrderNum=[%@]tranAmt=[%@]feeAmt=[%@]tranDateTime=[%@]frontMerUrl=[%@]backgroundMerUrl=[%@]orderId=[%@]gopayOutOrderId=[%@]tranIP=[%@]respCode=[%@]gopayServerTime=[%@]VerficationCode=[%@]", dic[@"version"], dic[@"tranCode"], dic[@"merchantID"], dic[@"merOrderNum"], dic[@"tranAmt"], dic[@"feeAmt"], dic[@"tranDateTime"], dic[@"frontMerUrl"], dic[@"backgroundMerUrl"], dic[@"orderId"], dic[@"gopayOutOrderId"], dic[@"tranIP"], dic[@"respCode"], dic[@"gopayServerTime"], dic[@"VerficationCode"]];

    NSLog(@"加密前的signValue:%@", signVaule);
    
    signVaule = [YSMD5 md5:signVaule];
    return signVaule;
}

// 创建mobileSignValue md5 加密字符串
+ (NSString *)mobileSignValueWithDictionary:(NSDictionary *)dic {
    
    NSString *mobileSignValue =
    [NSString stringWithFormat:@"version=[%@]tranCode=[%@]merchantID=[%@]merOrderNum=[%@]buyerContact=[%@]VerficationCode=[%@]", dic[@"version"], dic[@"tranCode"], dic[@"merchantID"], dic[@"merOrderNum"], dic[@"buyerContact"], dic[@"VerficationCode"]];
    
    NSLog(@"加密前的mobileSignValue:%@", mobileSignValue);
    
    mobileSignValue = [YSMD5 md5:mobileSignValue];
    return mobileSignValue;

}

+ (NSString *)getSystemCurrentTime {
    // 设置时间格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYYMMddHHmmss"];
    
    // 时间格式的时区为东八区
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    
    //转化为东八区的时间，即北京时间
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];
    
    return currentTime;
    
}


@end
